﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Text;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Service
{
    public class BellAccount
    {
        private EKAASBIMEntities dbContext;
        private static readonly object lock_flag = new object();
        public BellAccount()
        {
            dbContext = new EKAASBIMEntities();
        }
        public List<accountInBell> getAllBellAccount()
        {
            return dbContext.accountInBell.ToList();
        }
        public accountInBell getBellAccountByAlias(string alias)
        {
            var query = from a in dbContext.accountInBell
                        where a.belongto == alias
                        select a;
            return query.FirstOrDefault();
        }
        public void create(accountInBell account, out bool b)
        {
            b = false;
            var bellAccount = dbContext.accountInBell.SingleOrDefault(o => o.accountID == account.accountID || o.belongto == account.belongto);
            if (bellAccount != null)
            {
                b = true;
                return;
            }
            dbContext.accountInBell.AddObject(account);
            dbContext.SaveChanges();
        }
        public void delete(string[] ids)
        {
            foreach (string id in ids)
            {
                var d = dbContext.accountInBell.SingleOrDefault(o => o.accountID == id);
                if (d != null)
                {
                    dbContext.DeleteObject(d);
                }
            }

            dbContext.SaveChanges();
        }
        public void edit(accountInBell account, out bool b)
        {
            b = false;
            accountInBell old = dbContext.accountInBell.SingleOrDefault(o => o.accountID == account.accountID);
            if (old == null)
            {
                return;
            }
            else
            {
                if (old.belongto != account.belongto)
                {
                    var temp = dbContext.accountInBell.SingleOrDefault(o=>o.belongto==account.belongto);
                    if (temp != null)
                    {
                        b = true;
                        return;
                    }
                }
            }
            old.belongto = account.belongto;
            old.latestUpdateDate = account.latestUpdateDate;
            old.domain = account.domain;
            old.status = account.status;
            old.empNo = account.empNo;
            dbContext.SaveChanges();
        }
        public void batchUpdate(string[] IDlist, accountInBell a)
        {
            foreach (string s in IDlist)
            {
                var d = dbContext.accountInBell.SingleOrDefault(o => o.accountID == s);
                if (d != null)
                {
                    d.status = a.status;
                    d.latestUpdateDate = a.latestUpdateDate;
                }
            }
            dbContext.SaveChanges();
        }
        #region Auto mail
        public List<accountInBell> getPreOverdueAccInfo_By_Fixeddate(DateTime date1, DateTime date2)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.accountInBell
                            where n.status == 2 || (n.latestUpdateDate > date2 && n.latestUpdateDate < date1)
                            select n;
                return query.ToList();
            }
        }
        public List<accountInBell> getOverduedAccInfo_By_Fixeddate(DateTime date)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.accountInBell
                            where n.status == 3 || n.latestUpdateDate < date
                            select n;
                return query.ToList();
            }
        }
        public void update_AccStatus_By_AccID(string id, byte status)
        {
            lock (lock_flag)
            {
                accountInBell acc = dbContext.accountInBell.SingleOrDefault(o => o.accountID == id);
                if (acc == null)
                {
                    return;
                }
                if (acc.status != status)
                {
                    acc.status = status;
                    dbContext.SaveChanges();
                }
            }
        }
        public BellAccountInfo_mail getAccInfoForMail_By_ID(string id)
        {
            lock (lock_flag)
            {
                var query = from u in dbContext.aspnet_Users
                            join m in dbContext.aspnet_Membership on u.UserId equals m.UserId
                            join a in dbContext.accountInBell on u.UserName equals a.belongto
                            where a.accountID == id
                            select new BellAccountInfo_mail
                            {
                                AccID = a.accountID,
                                AccID2 = a.empNo,
                                owner = u.UserName,
                                email = m.Email
                            };
                return query.FirstOrDefault();
            }
        }
        #endregion
    }
}
